#coding:utf-8
import scrapy
import json
import csvout

class QuotesSpider(scrapy.Spider):
    name = "csdn"

    def start_requests(self):
        url = 'http://so.csdn.net/so/search/s.do'
        tag = getattr(self, 'tag', None)
        if tag is not None:
            url1 = url + '?q=' + tag + '&t=blog'
            yield scrapy.Request(url1, meta={'na':'csdn_blog_num'}, callback=self.parse)
            url2 = url + '?q=' + tag + '&t=discuss&o=simi&s=&l=null'
            yield scrapy.Request(url2, meta={'na': 'csdn_discuss_num'}, callback=self.parse)

    def parse(self, response):
        res = response.css("span[class='page-nav'] span[class='text']::text").extract_first()
        #unicode字符串，取出'共‘和’条'之间的数字
        num = res.split(u'共')[1].split(u'条')[0]
        num = int(num)
        outli = [response.meta['na'], num, response.url]
        csvout.list2csv(outli)
        yield {
            response.meta['na']: num
        }
